﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.Linq;
using Microsoft.Phone.UserData;
using System.IO;
using System.ComponentModel;
using System.Threading;
using QuickTasks;
using QuickTasks.ViewModels.Tables;
using Microsoft.Phone.Shell;

namespace BackAgent
{

    public class updatecontacts
    {
        public static int cnt;
        public static AutoResetEvent auto = new AutoResetEvent(false);

        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
         QuickTaskDataContext context=new QuickTaskDataContext("isostore:/quick.sdf");

         string name;
        string Mphone ;
        string Hphone ;
        string Wphone;
        string Personal;
        string work;
        string other;

        public void check()
        {
                var xqueryfavlist1 = from i in context.people select i;
            cnt = xqueryfavlist1.Count();
            if (cnt != 0)
            {
                if (settings.Contains("cnt1"))
                {
                   
                }
                else
                {
                    settings.Add("cnt1", "1");
                }
                settings.Save();
                if (settings.Contains("cnt"))
                {



                    if (Convert.ToInt32(settings["cnt1"].ToString()) + 1 > cnt)
                    {
                        settings["cnt"] = settings["cnt1"].ToString();
                    }
                    else
                    {
                        settings["cnt"] = (Convert.ToInt32(settings["cnt1"]) + 1).ToString();
                    }
                    settings.Save();

                }
                else
                {
                    settings.Add("cnt", "1");
                }
                settings.Save();


               int j = Convert.ToInt32(settings["cnt1"].ToString());
                
                    var xqueryfavlist = from i in context.people where i.id.ToString() == j.ToString() select i;

                    foreach (var objlist in xqueryfavlist)
                    {

                        name = objlist.name;
                        Contacts objContacts = new Contacts();

                        objContacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(objContacts_SearchCompleted);

                        objContacts.SearchAsync(objlist.name, FilterKind.DisplayName, objlist.id.ToString());
                        auto.WaitOne();
                        IQueryable<people> xqueryfavlists = from i in context.people where i.id.ToString() == j.ToString() select i;
                        people p = xqueryfavlists.FirstOrDefault();
                        {

                            if (p.mobile != Mphone)
                                p.mobile = Mphone;

                            if (p.home != Hphone)
                                p.home = Hphone;

                            if (p.mwork != Wphone)
                                p.mwork = Wphone;

                            if (p.personal != Personal)
                                p.personal = Personal;

                            if (p.work != work)
                                p.work = work;

                            if (p.other != other)
                                p.other = other;


                        }

                        context.SubmitChanges();
                        if (cnt.ToString() == settings["cnt1"].ToString())
                        {
                            settings["cnt1"] = "1";
                        }
                        else
                        {
                            settings["cnt1"] = (j + 1).ToString();
                        }
                        settings.Save();
                        break;
                   

                }
               
            }
          
        }

        public void objContacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
           
                foreach (var result in e.Results)
                {
                    if (name == result.DisplayName.ToString())
                    {
                        var ph = result.PhoneNumbers.ToArray();
                        var email = result.EmailAddresses.ToArray();
                        if (ph != null)
                        {
                            int k = ph.Count();
                            if (k == 0)
                            {
                            }
                            else
                            {
                                foreach (var s in ph)
                                {
                                    if (s.Kind.ToString() == "Mobile")

                                        Mphone = s.PhoneNumber;


                                    if (s.Kind.ToString() == "Home")

                                        Hphone = s.PhoneNumber;

                                    if (s.Kind.ToString() == "Work")

                                        Wphone = s.PhoneNumber;

                                }
                            }
                        }

                        if (email != null)
                        {
                            int k1 = email.Count();
                            if (k1 == 0)
                            {
                            }
                            else
                            {
                                foreach (var s in email)
                                {
                                    if (s.Kind.ToString() == "Personal")

                                        Personal = s.EmailAddress;

                                    if (s.Kind.ToString() == "work")

                                        work = s.EmailAddress;

                                    if (s.Kind.ToString() == "other")

                                        other = s.EmailAddress;

                                }
                            }
                        }
                        break;
                    }
                    
                }
                auto.Set();
           
        }
    }
}
    


